package com.sweets.look.ui.setting;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.sweet.satellite.GlobalConfig;
import com.sweet.satellite.TranslucentSBActivity;
import com.sweets.look.R;
import com.sweets.look.constant.Constants;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.widget.MarkMultiChoiceDialog;
import com.sweets.look.util.BrowserUtils;
import com.sweets.look.util.LogUtils;
import com.sweets.look.util.Utils;
import com.sweets.look.util.ViewUtils;

public class PrivacySettings extends TranslucentSBActivity {
    private final static String TAG = PrivacySettings.class.getSimpleName();

    private CheckBox mCbBlock3rdPartyCookies;
    private CheckBox mCbDoNotTrack;
    private CheckBox mCbLocation;
    private CheckBox mCbRemoveIdentifyingHeaders;
    private CheckBox mCbSaveFormdata;
    private CheckBox mCbSavePassword;
    private Context mContext;
    private PreferenceManager mPreferences;

    private void clearData() {
        new MarkMultiChoiceDialog(this.mContext).builder()
                .setTitle(this.getResources().getString(R.string.clear_data))
                .setMessage(this.getResources().getString(R.string.cannot_undo)).setMultiChoiceItems(this.getResources()
                .getStringArray(R.array.array_data), new boolean[]{true, true, true, true, false}, this.getResources()
                .getString(R.string.action_ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String v2 = String.valueOf(which).substring(1);
                int v1 = v2.length();
                int v0;
                for (v0 = 0; v0 < v1; ++v0) {
                    if (v2.substring(v0, v0 + 1).equalsIgnoreCase("1")) {
                        switch (v0) {
                            case 0: {//缓存数据包括,磁盘数据
                                BrowserUtils.clearWebViewCache(mContext);
                                LogUtils.w(TAG, "clear web view cache");
                                break;
                            }
                            case 1: {//表单数据
                                BrowserUtils.clearWebViewFormData(mContext);
                                LogUtils.w(TAG, "clear web view form data");
                                break;
                            }
                            case 2: {//历史
                                BrowserUtils.clearWebViewHistory(mContext);
                                LogUtils.w(TAG, "clear web view history");
                                break;
                            }
                            case 3: {//网页存储
                                BrowserUtils.clearWebStorage();
                                LogUtils.w(TAG, "clear web view web storage");
                                break;
                            }
                            case 4: {//Cookies
                                BrowserUtils.clearWebViewCookies(mContext);
                                LogUtils.w(TAG, "clear web view cookies");
                                break;
                            }
                        }

                    }

                }

                ViewUtils.showToast(findViewById(R.id.settingLayout), getResources()
                        .getString(R.string.data_cleared));
            }
        }).setNegativeButton(this.getResources().getString(R.string.action_no), null).show();
    }

    private void initBlock3rdPartyCookiesPreference() {
        if (Constants.API < 21) {
            this.findViewById(R.id.layoutBlock3rdPartyCookies).setVisibility(View.GONE);
        }

        this.mCbBlock3rdPartyCookies = (CheckBox) this.findViewById(R.id.cbBlock3rdPartyCookies);
        this.mCbBlock3rdPartyCookies.setChecked(this.mPreferences.getBlock3rdPartyCookiesEnabled());
        this.mCbBlock3rdPartyCookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener
                () {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setBlock3rdPartyCookiesEnabled(isChecked);
            }
        });
    }

    private void initDoNotTrackPreference() {
        this.mCbDoNotTrack = (CheckBox) this.findViewById(R.id.cbDoNotTrack);
        this.mCbDoNotTrack.setChecked(this.mPreferences.getDoNotTrackEnabled());
        this.mCbDoNotTrack.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setDoNotTrackEnable(isChecked);
            }
        });
    }

    private void initLocationPreference() {
        this.mCbLocation = (CheckBox) this.findViewById(R.id.cbLocation);
        this.mCbLocation.setChecked(this.mPreferences.getLocationEnabled());
        this.mCbLocation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setLocationEnabled(isChecked);
            }
        });
    }

    private void initRemoveIdentifyingHeadersPreference() {
        this.mCbRemoveIdentifyingHeaders = (CheckBox) this.findViewById(R.id.cbRemoveIdentifyingHeaders);
        this.mCbRemoveIdentifyingHeaders.setChecked(this.mPreferences.getRemoveIdentifyingHeadersEnabled());
        this.mCbRemoveIdentifyingHeaders.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener
                () {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setRemoveIdentifyingHeadersEnabled(isChecked);
            }
        });
    }

    private void initSaveFormdataPreference() {
        this.mCbSaveFormdata = (CheckBox) this.findViewById(R.id.cbSaveFormdata);
        this.mCbSaveFormdata.setChecked(this.mPreferences.getSaveFormDataEnabled());
        this.mCbSaveFormdata.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setSaveFormdataEnabled(isChecked);
            }
        });
    }

    private void initSavePasswordPreference() {
        if (Constants.API >= 19) {
            this.findViewById(R.id.layoutSavePassword).setVisibility(View.GONE);
        } else {
            this.mCbSavePassword = (CheckBox) this.findViewById(R.id.cbSavePassword);
            this.mCbSavePassword.setChecked(this.mPreferences.getSavePasswordsEnabled());
            this.mCbSavePassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener
                    () {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mPreferences.setSavePasswordsEnabled(isChecked);
                }
            });
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.settings_privacy);
        this.setToolbarTitle(R.string.settings_privacy);

        this.mPreferences = PreferenceManager.getInstance();
        this.mContext = this;
        this.initLocationPreference();
        this.initBlock3rdPartyCookiesPreference();
        this.initDoNotTrackPreference();
        this.initRemoveIdentifyingHeadersPreference();
        this.initSaveFormdataPreference();
        this.initSavePasswordPreference();
        ViewUtils.swipeToFinish(this.findViewById(R.id.settingLayout), this);
    }

    public void onSettingsItemClick(View v) {
        CheckBox v2;
        boolean v0 = true;
        switch (v.getId()) {
            case R.id.layoutLocation: {//允许站点访问地理位置
                v2 = this.mCbLocation;
                if (this.mCbLocation.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutBlock3rdPartyCookies: {//拦截第三方Cookies
                v2 = this.mCbBlock3rdPartyCookies;
                if (this.mCbBlock3rdPartyCookies.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutDoNotTrack: {//不跟踪
                v2 = this.mCbDoNotTrack;
                if (this.mCbDoNotTrack.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutRemoveIdentifyingHeaders: {//向网站隐藏身份信息
                v2 = this.mCbRemoveIdentifyingHeaders;
                if (this.mCbRemoveIdentifyingHeaders.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutSaveFormdata: {//记录表单
                v2 = this.mCbSaveFormdata;
                if (this.mCbSaveFormdata.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutSavePassword: {//保存密码
                v2 = this.mCbSavePassword;
                if (this.mCbSavePassword.isChecked()) {
                    v0 = false;
                }

                v2.setChecked(v0);
                break;
            }
            case R.id.layoutClearData: {//清除数据
                this.clearData();
                break;
            }
            case R.id.layoutClearDataOnExit: {//退出时清除
                this.setClearDataOnExit();
                break;
            }
        }
    }


    private void setClearDataOnExit() {
        new MarkMultiChoiceDialog(this.mContext).builder().setTitle(this.getResources().getString(R.string.clear_data_on_exit))
                .setMultiChoiceItems(this.getResources().getStringArray(R.array.array_data),
                        new boolean[]{this.mPreferences.getClearCacheExit(),
                                this.mPreferences.getClearFormdataExitEnabled(),
                                this.mPreferences.getClearHistoryExitEnabled(),
                                this.mPreferences.getClearWebStorageExitEnabled(),
                                this.mPreferences.getClearCookiesExitEnabled()},
                        this.getResources().getString(R.string.action_ok), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String v3 = String.valueOf(which).substring(1);
                                int size = v3.length();
                                for (int i = 0; i < size; ++i) {
                                    boolean v0 = v3.substring(i, i + 1).equalsIgnoreCase("1");
                                    switch (i) {
                                        case 0: {
                                            mPreferences.setClearCacheExit(v0);
                                            break;
                                        }
                                        case 1: {
                                            mPreferences.setClearFormdataExitEnabled(v0);
                                            break;
                                        }
                                        case 2: {
                                            mPreferences.setClearHistoryExitEnabled(v0);
                                            break;
                                        }
                                        case 3: {
                                            mPreferences.setClearWebStorageExitEnabled(v0);
                                            break;
                                        }
                                        case 4: {
                                            mPreferences.setClearCookiesExitEnabled(v0);
                                            break;
                                        }
                                    }
                                }
                            }
                        })
                .setNegativeButton(this.getResources().getString(R.string.action_no), null).show();
    }
}

